package com.example.sys.ai.dto;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;

import java.io.Serializable;

@Data
public class IDCard implements Serializable {

    public static final String WORDS_RESULT = "words_result";

    public static final String WORDS = "words";

    public static final String NAME = "姓名";

    public static final String NATION = "民族";

    public static final String ADDRESS = "住址";

    public static final String CODE = "公民身份号码";

    public static final String EXPIRING = "失效日期";

    /***
     * 正面信息
     */
    @Data
    public class Front implements Serializable {

        //姓名
        private String name;

        //民族
        private String nation;

        //住址
        private String address;

        //身份证号
        private String code;

    }

    /***
     * 背面信息
     */
    @Data
    public class Back implements Serializable {

        //过期时间
        private String expiring;
    }

    /***
     * 身份证正面
     * @param json
     * @return
     */
    public IDCard.Front toFront(String json) {
        IDCard.Front dto = new IDCard.Front();
        JSONObject obj = JSONObject.parseObject(json);
        if (obj.containsKey(WORDS_RESULT)) {
            JSONObject wordsResult = obj.getJSONObject(WORDS_RESULT);
            if (wordsResult.containsKey(NAME)) {
                dto.setName(wordsResult.getJSONObject(NAME).getString(WORDS));
            }
            if (wordsResult.containsKey(NATION)) {
                dto.setNation(wordsResult.getJSONObject(NATION).getString(WORDS));
            }
            if (wordsResult.containsKey(ADDRESS)) {
                dto.setAddress(wordsResult.getJSONObject(ADDRESS).getString(WORDS));
            }
            if (wordsResult.containsKey(CODE)) {
                dto.setCode(wordsResult.getJSONObject(CODE).getString(WORDS));
            }
        }
        return dto;
    }

    /***
     * 身份证反面
     * @param json
     * @return
     */
    public IDCard.Back toBack(String json) {
        IDCard.Back dto = new IDCard.Back();
        JSONObject obj = JSONObject.parseObject(json);
        if (obj.containsKey(WORDS_RESULT)) {
            JSONObject wordsResult = obj.getJSONObject(WORDS_RESULT);
            if (wordsResult.containsKey(EXPIRING)) {
                dto.setExpiring(wordsResult.getJSONObject(EXPIRING).getString(WORDS));
            }
        }
        return dto;
    }
}
